package com.xxx.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class ZooKeeperConfig {

    @Value("${zookeeper.address}")
    private String zookeeperAddress;

    @Value("${zookeeper.namespace}")
    private String namespace;

    @Bean
    public CuratorFramework curatorFramework() {
        // 重试策略：初始等待 1s，最多重试 3 次
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

        // 创建 Curator 客户端
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(zookeeperAddress)
                .namespace(namespace)  // 命名空间（可选）
                .retryPolicy(retryPolicy)
                .build();

        client.start();  // 启动连接
        log.info("zookeeper client started success!!!");
        return client;
    }
}